# -*- coding: utf-8 -*-

import sys 	
from enum import Enum
# 各属性项枚举
class EAttrIndex (Enum) :
        EAttr_Id = 0       			# id
        EAttr_Descrition = 1		# 描述
        Eattr_Max = 2

    # 速度枚举值
class EAnimateSpeed (Enum) :
        EAnimateSpeed_Slow = 0		# 慢速
        EAnimateSpeed_Normal = 1	# 中速
        EAnimateSpeed_Fast = 2		# 快速
        EAnimateSpeed_Max = 3

class CDelegate(QStyledItemDelegate):
	def __init__(self, parent=None) :
		super(CMainDialog, self).__init__(parent)
		self.setupUi(self)
		strListYesNo=['yes', 'no'] # 0：yes, 1:no
		strListSpeed=['慢速', '中速', '快速'] # 0：慢速, 1:中速, 2：快速

	def createEditor(parent, option, index) :
		# 只有第1列允许编辑
		if 1 != index.column() :
			return QStyledItemDelegate.createEditor(parent, option, index)
		if EAttr_Id == index.row() :
			editor = QSpinBox(parent)
			editor.setFrame(False)
			editor.setMinimum(0)
			editor.setMaximum(100)
			return editor
		elif EAttr_Checked == index.row():
			QComboBox* editor = new QComboBox(parent)
			editor.addItems(m_strListYesNo)
			editor.setItemData(0, 0) # 0:yes,序号=0，对应的值=0
			editor.setItemData(1, 1) # 1:no,序号=1，对应的值=1
			return editor
		elif EAttr_LastOneFlag == index.row():
        CEditor *editor = new CEditor(parent)
        connect(editor, &CEditor.sig_editFinished,
                this, &CDelegate.slot_commitAndCloseEditor)
        return editor

    }
    elifEattr_AnimateSpeed == index.row()){
        QComboBox* editor = new QComboBox(parent)
        editor.addItems(m_strListSpeed)
        editor.setItemData(0, EAnimateSpeed_Slow)
        editor.setItemData(1, EAnimateSpeed_Normal)
        editor.setItemData(2, EAnimateSpeed_Fast)
        return editor
    }
    else {
        return QStyledItemDelegate.createEditor(parent,
                                                 option,
                                                 index)
    }
}

void CDelegate.setEditorData(QWidget *editor, const QModelIndex &index) const{
    if (1 != index.column():
        return QStyledItemDelegate.setEditorData(editor, index)
    }
    if (EAttr_Id == index.row()){
        QSpinBox* editor = dynamic_cast<QSpinBox*>(editor)
        int value = index.model().data(index, Qt.EditRole).toInt()
        editor.setValue(value)
    }
    elifEAttr_Checked == index.row()){
        QComboBox* editor = dynamic_cast<QComboBox*>(editor)
        #      index  value(data())
        # yes  0        0
        # no   1        1
        int idx = (index.model().data(index, Qt.EditRole).toBool() ? 1 : 0)
        editor.setCurrentIndex(idx)
    }
    elifEAttr_LastOneFlag == index.row():
        CEditor* editor = dynamic_cast<CEditor*>(editor)
        QVariant var = index.model().data(index, Qt.EditRole)
        Qt.CheckState checkState = static_cast<Qt.CheckState>(var.toInt())
        if (checkState:
            editor.setText(QString.fromLocal8Bit("是"))
        }
        else {
            editor.setText(QString.fromLocal8Bit("否"))
        }
    }
    elifEattr_AnimateSpeed == index.row()){
        QComboBox* editor = static_cast<QComboBox*>(editor)
        int value = index.model().data(index, Qt.EditRole).toInt() #
       # EAnimateSpeed eSpeed =
       #         static_cast<EAnimateSpeed>(value)
        editor.setCurrentIndex(value)
    }
    else {
        QStyledItemDelegate.setEditorData(editor, index)
    }

}

void CDelegate.setModelData(QWidget *editor,
                  QAbstractItemModel *model,
                  const QModelIndex &index) const {
    if (1 != index.column()){
        return QStyledItemDelegate.setModelData(editor, model, index)
    }
    QVariant var
    if (EAttr_Id == index.row()){
        QSpinBox* editor = qobject_cast<QSpinBox*>(editor)
        editor.interpretText()
        model.setData(index, editor.value(), Qt.EditRole)
    }
    elifEAttr_Checked == index.row()){
        QComboBox* editor = qobject_cast<QComboBox*>(editor)
        var = editor.currentData()# 0:yes, 1:no
        model.setData(index, var.toBool())
    }
    elifEAttr_LastOneFlag == index.row():
        CEditor* editor = qobject_cast<CEditor*>(editor)
        var.setValue((editor.text()==QString.fromLocal8Bit("是")) ? true : false)
        model.setData(index, var)
    }
    elifEattr_AnimateSpeed == index.row()){
        QComboBox* editor = qobject_cast<QComboBox*>(editor)
        var = editor.currentData()
        model.setData(index, var)
    }
    else {
        QStyledItemDelegate.setModelData(editor, model, index)
    }
}

void CDelegate.updateEditorGeometry(QWidget *editor,
                          const QStyleOptionViewItem &option,
                          const QModelIndex &index) const {

    Q_UNUSED(index)
    editor.setGeometry(option.rect)
}
